<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #box1 {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
<button id="btn1">点击</button>
<br/>
<div id="box1" style="width: 200px; height: 200px; background-color: black;"></div>

<script>
    window.onload = function () {
        var btn1 = document.getElementById("btn1");
        var box1 = document.getElementById("box1");

        /**
         * 获取元素当前显示的样式，兼容不同浏览器
         * @param {HTMLElement} obj - 要获取样式的元素
         * @param {string} name - 样式属性名
         * @returns {string} - 返回样式值
         */
        function getStyle(obj, name) {
            if (typeof name !== 'string') return '';
            if (window.getComputedStyle) {
                return getComputedStyle(obj, null)[name];
            } else {
                // 兼容 IE8 及以下版本
                return obj.currentStyle[name];
            }
        }

        // 示例调用
        btn1.onclick = function () {
            alert(getStyle(box1, 'backgroundColor')); // 输出计算后的背景色
        };
    }
</script>
</body>
</html>
